package com.example.module_notice.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.example.module_notice.R
import com.example.module_notice.databinding.NoticeHomeItemBinding
import com.example.module_notice.ui.model.AnnouncementEntityItem

class NoticeAdapter : RecyclerView.Adapter<NoticeAdapter.ViewHolder>() {
    private val mList = mutableListOf<AnnouncementEntityItem>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = NoticeHomeItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return  ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.post = mList[position]
        holder.button.setOnClickListener {
            gotoDetail(mList[position])
        }
    }

    fun setList(listItem : MutableList<AnnouncementEntityItem>){
        mList.clear()
        mList.addAll(listItem)
        notifyDataSetChanged()
    }


    inner class ViewHolder(val binding : NoticeHomeItemBinding) : RecyclerView.ViewHolder(binding.root){
        val button= itemView.findViewById<TextView>(R.id.notice_button)
    }

    fun gotoDetail(item : AnnouncementEntityItem){
        if (item.status == "未读"  ||  item.status == "未阅读"){
            // 正确ARouter跳转语法（移除 path: 参数名）
            ARouter.getInstance()
                .build("/notice/detail")
                .withString("notice_id", ""+item.id) // 建议传递参数
                .navigation()

            item.status = "已读"
            notifyItemChanged(mList.indexOf(item)) // 更新单项
        }else{
            Log.d("Notice----", "gotoDetail: ${item.status}")
        }
    }

}

